package com.chinatechstar.notification.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinatechstar.notification.entity.SysMsg;
import com.chinatechstar.notification.mapper.SysMsgMapper;
import com.chinatechstar.notification.service.SysMsgService;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 业务逻辑实现层
 */
@Service
@Transactional
public class SysMsgServiceImpl implements SysMsgService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final SequenceGenerator SEQUENCE_GENERATOR = new SequenceGenerator();

    @Autowired
    private SysMsgMapper sysMsgMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询所有数据
     */
    @Override
    public List<SysMsg> findSysMsgAll() {
        return sysMsgMapper.findSysMsgAll();
    }

    /**
     * 查询分页
     */
    @Override
    public Map<String, Object> findSysMsgByCondition(Integer currentPage, Integer pageSize, String name, String sorter) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        if (StringUtils.isNotBlank(sorter)) {
            String sort = sorter.substring(0, sorter.lastIndexOf('_'));
            String sequence = "ascend".equals(sorter.substring(sorter.lastIndexOf('_') + 1)) ? "ASC" : "DESC";
            paramMap.put("sort", sort);
            paramMap.put("sequence", sequence);
        } else {
            paramMap.put("sort", "createTime");
            paramMap.put("sequence", "DESC");
        }
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList = sysMsgMapper.findSysMsgByCondition(paramMap);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), currentPage, pageSize);
    }

    /**
     * 查询导出数据列表
     */
    @Override
    public List<LinkedHashMap<String, Object>> findSysMsgForExcel(Map<String, Object> paramMap) {
        return sysMsgMapper.findSysMsgByCondition(paramMap);
    }

    /**
     * 根据主键ID数据查询数据
     */
    @Override
    public SysMsg findSysMsgById(Long id) {
        return sysMsgMapper.findSysMsgById(id);
    }

    /**
     * 根据主键ID数组查询数据
     */
    @Override
    public List<SysMsg> findSysMsgByIds(Long[] ids) {
        return sysMsgMapper.findSysMsgByIds(ids);
    }

    /**
     * 新增
     */
    @Override
    public void insertSysMsg(SysMsg sysMsg) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        sysMsg.setId(SEQUENCE_GENERATOR.nextId());
        sysMsgMapper.insertSysMsg(sysMsg);
        logger.info("已新增： {}", sysMsg.getId());
    }

    /**
     * 批量新增
     */
    @Override
    public void insertSysMsgs(List<SysMsg> sysMsgs) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        for (SysMsg sysMsg : sysMsgs) {
            sysMsg.setId(SEQUENCE_GENERATOR.nextId());
        }
        sysMsgMapper.insertSysMsgs(sysMsgs);
    }

    /**
     * 编辑
     */
    @Override
    public void updateSysMsg(SysMsg sysMsg) {
        sysMsgMapper.updateSysMsg(sysMsg);
        logger.info("已编辑： {}", sysMsg.getId());
    }

    /**
     * 批量编辑
     */
    @Override
    public void updateSysMsgs(List<SysMsg> sysMsgs) {
        sysMsgMapper.updateSysMsgs(sysMsgs);
    }

    /**
     * 删除
     */
    @Override
    public void deleteSysMsgById(Long id) {
        sysMsgMapper.deleteSysMsgById(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteSysMsgByIds(Long[] ids) {
        sysMsgMapper.deleteSysMsgByIds(ids);
    }

}
